﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunnyFunction
{
    public class Pokers
    {
        private static string[] nums = new string[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
        private static Suit[] suits = new Suit[] { Suit.diamonds, Suit.flowers, Suit.hearts, Suit.spades };
        public const string joker = "joker";
        public const string JOKER = "JOKER";

        public List<Poker> pList { get; set; }

        internal List<Poker> SendCount(int v)
        {
            var result = pList.Take(5).ToList();
            pList = pList.Skip(5).ToList();
            return result;
        }

        public static Pokers Init(int pCount = 1)
        {
            Pokers pks = new Pokers();

            pks.pList = new List<Poker>();
            for (int i = 0; i < pCount; i++)
            {
                foreach (var suit in suits)
                {
                    foreach (var num in nums)
                    {
                        pks.pList.Add(new Poker() { num = num, suit = suit });
                    }
                }
                pks.pList.Add(new Poker() { suit = Suit.none, num = joker });
                pks.pList.Add(new Poker() { suit = Suit.none, num = JOKER });
            }

            return pks;
        }

        public void Shuffle()
        {
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            for (int i = pList.Count; i > 0; i--)
            {
                var num = rand.Next(0, i);

                var tp = pList[num];
                pList[num] = pList[i - 1];
                pList[i - 1] = tp;
            }
        }

        public void Cut()
        {
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            var num = rand.Next(0, pList.Count);

            var temp1 = pList.Take(num);
            var temp2 = pList.Skip(num).ToList();
            temp2.AddRange(temp1);
            pList = temp2;
        }

        public List<List<Poker>> Send(int count)
        {
            List<List<Poker>> list = new List<List<Poker>>();
            for (int i = 0; i < count; i++)
            {
                list.Add(new List<Poker>());
            }
            int num = 0;
            for (int i = 0; i < pList.Count; i++)
            {
                list[num++].Add(pList[i]);
                if (num >= count)
                {
                    num = 0;
                }
            }

            return list;
        }
    }

    public class Poker
    {
        public Suit suit { get; set; }

        public string num { get; set; }
    }

    public enum Suit
    {
        hearts, 
        flowers, 
        diamonds,
        spades,
        none,
    }
}
